package com.hnkywl.sts.webservice.bg.util;

import java.security.MessageDigest;

import org.apache.log4j.Logger;

public class EncryptUtil {

	private static Logger log = Logger.getLogger(EncryptUtil.class);

	/**
	 * BASE64 解密算法
	 * @param dCodeString
	 * @return
	 */
	public static String deCrypt(String dCodeString) {
		String tmp = "";
		try {
			if (dCodeString != null) {
				tmp = Encrypt.deCrypt(dCodeString.getBytes());
			}
		} catch (Exception e) {
			 log.error("Encrypt dCode error : ", e);
		}
		return tmp;

	}

	/**
	 * BASE 64 加密算法
	 * @param eCodeString
	 * @return
	 */
	public static String enCrypt(String eCodeString) {
		String tmp = "";
		try {
			if (eCodeString != null) {
				tmp = Encrypt.enCrypt(eCodeString);
			}
		} catch (Exception e) {
			 log.error("Encrypt eCode error : ", e);
		}
		return tmp;

	}
	
	/**
	 * MD5 加密算法
	 * @param plainText 明文
	 * @return MD5 密文
	 */
	public static String MD5(String plainText) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};       
        try {
            byte[] btInput = plainText.getBytes("UTF-8");
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {

        String s = "testGwDTBoV6wTQ=121212";

        System.out.println(MD5(s));
    }
}
